package com.asman.vr.network

import androidx.lifecycle.MutableLiveData
import com.asman.vr.dto.BaseRes
import com.asman.vr.dto.BaseResNull
import retrofit2.Call

/**
 * @author ybk
 * @date :2020/2/3 11:10 PM
 * @desc:
 */
fun <T> liveData(api: Call<BaseRes<T>>): MutableLiveData<Resource<T>> {
    val liveData = MutableLiveData<Resource<T>>()
    liveData.value = Resource.loading()
    api.enqueue(object : MyRetrofitCallback<T>() {
        override fun onSuccess(
            call: Call<BaseRes<T>>, result: T, datetime: String
        ) {
            liveData.postValue(Resource.success(result, datetime))
        }

        override fun onError(
            errorType: ErrorType,
            call: Call<BaseRes<T>>,
            code: String,
            msg: String
        ) {
            liveData.postValue(Resource.error(code, msg))
        }
    })
    return liveData
}

fun liveDataNull(api: Call<BaseResNull>): MutableLiveData<ResourceNull> {
    val liveData = MutableLiveData<ResourceNull>()
    liveData.value = ResourceNull.loading()
    api.enqueue(object : MyRetrofitCallbackNull() {

        override fun onSuccess(call: Call<BaseResNull>, datetime: String) {
            liveData.postValue(ResourceNull.success(datetime))
        }

        override fun onError(
            errorType: ErrorType,
            call: Call<BaseResNull>,
            code: String,
            msg: String
        ) {
            liveData.postValue(ResourceNull.error(code, msg))
        }
    })
    return liveData
}
